var health=health||{};
var role;


health.Health=function(){
	var s = this;
    health.container = s;
	F2xContainer.call(s);
	if(window){
		window.Health = this;
	}
    s.initUI();


};
F2xExtend(health.Health,F2xContainer);
health.Health.prototype.initUI=function(){
	var s = this;
	//f2x_auto_created_init_start
	var _d0=new health.anchor();
	Flash2x.d(_d0,{x:88.9,y:43.1});
	var _d1=new health.bgimage();
	_d1.name="bgimage";
	s.bgimage=_d1;
	Flash2x.d(_d1,{x:-11,y:-13});
	s.addChild(_d1);
	//s.addChild(_d0);
	//f2x_auto_created_init_end

};


(function(global){
   var $api = global.$api || {};
	 var role = null;
	 var StatusMap = {
		 CHECKING: 'is_checking',
		 STOPPED: 'is_stopped',
		 PAUSED: 'is_paused',
	 };

	 var EventMap = {
		 START: 'start',
		 PAUSE: 'pause',
		 RESUME: 'resume',
		 STOP: 'stop',
		 COMPLETE: 'complete'
	 }


	 var CURRENT_STATUS = StatusMap.STOPPED;
	 var interval;



    function createRole(type, position){
        var type = type || 'man';
        var position = position || {x:0, y:0};
        var positionX = position.x || 0;
        var positionY = position.y || 0;


        var container = health.container;

				if(role){
					reset();
				}

        if(positionX > 0 && positionX < 1){
            positionX = positionX * container.stage.divWidth;
        }

        if(positionY > 0 && positionY < 1){
            positionY = positionY * container.stage.divHeight;
        }



        switch(type){
            case 'man':
                role =  new health.Man();
                role.x = positionX;
                role.y = positionY;
                break;
            case 'woman':
                role =  new health.Woman();
                role.x = positionX;
                role.y = positionY;
                break;
            case 'ruler':
                role = new health.Ruler();
                role.x = positionX;
                role.y = positionY;
                break;
            default:
                trace('empty is created!');
                return null;
        }

        if(role){
            container.addChild(role);
			  }

        if(role instanceof annie.MovieClip){
            //role.__proto__.start = startCheck;
						role.__proto__.checkHealth = checkHealth;
				}else{
            role.__proto__.start = setValue;
        }


        return role;
    }
    function checkHealth(time){
			switch (CURRENT_STATUS) {
				case StatusMap.STOPPED:
				   start(time);
					 break;

				case StatusMap.CHECKING:
					pause();
					break;

				case StatusMap.PAUSED:
					resume(time);
					break;
				default:

			}

		}


		function start(time){
			var time = time || 3000;
			if(!interval){
				  role.dot.play();
					role.nextFrame();
			}
			interval = setInterval(function(){
					role.nextFrame();
					if(role.currentFrame == role.totalFrames){
							role.dot.stop();
							clearInterval(interval);
							role.gotoAndStop(1);
							role.dispatchEvent(EventMap.COMPLETE);
							CURRENT_STATUS = StatusMap.STOPPED;
					}

			},time);

			CURRENT_STATUS = StatusMap.CHECKING;
			role.dispatchEvent(EventMap.START);
		}

		function pause(){
			role.dot.stop();
			if(interval) clearInterval(interval);

			CURRENT_STATUS = StatusMap.PAUSED;
			role.dispatchEvent(EventMap.PAUSE);
		}

		function resume(time){
			var time = time || 3000;
			role.dot.play();

			interval = setInterval(function(){
					role.nextFrame();
					if(role.currentFrame == role.totalFrames){
							role.dot.stop();
							clearInterval(interval);
							role.gotoAndStop(1);
							role.dispatchEvent(EventMap.COMPLETE);
							CURRENT_STATUS = StatusMap.STOPPED;
					}

			},time);


			CURRENT_STATUS = StatusMap.CHECKING;
			role.dispatchEvent(EventMap.RESUME);
		}

		function reset(){
			if(interval) clearInterval(interval);
			if(role){
				role.dispatchEvent(EventMap.COMPLETE);
				health.container.removeChild(role);
				rolen = null;
			}
			CURRENT_STATUS = StatusMap.STOPPED;

		}




    function setValue(value){
        this.dispatchEvent('start');
        //this.height = (value -25)/(45-25) * this.height;
        this.line.scaleY = (value -25)/(45-25);
        trace('start take temperature');
    }



    $api.create = createRole;

    if(!global.$api){
        global.$api = $api;
    }
    //trace(global.$api);
    return global;

})(window);
